package com.jy.appmarket.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;

import com.jy.appmarket.R;

public class BitmapTools {

	private final static int MAX_ICON_NUM = 9;
	
	public static void saveBitmap2File(Context ctx, Bitmap bitmap, String name) {
		FileOutputStream out = null;
		try {
			out = ctx.openFileOutput(name, Context.MODE_MULTI_PROCESS);
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
			out.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 读外部存储空间里面的文件来获取Bitmap，一般用于读SD卡里面的文件.
	 * @param 文件的绝对路径名。
	 * @return Bitmap
	 */
	public static Bitmap getBitmapFromFile(String path) {
		return BitmapFactory.decodeFile(path);
	}
	
	/**
	 * 读程序内部存储空间里面的文件来获取Bitmap.
	 * @ctx 上下文。
	 * @name 文件名，不能包含路径符号。
	 * @return Bitmap
	 */
	public static Bitmap getBitmapFromFile(Context ctx, String name) {
		FileInputStream fis = null;
		Bitmap bm = null;
		try {
			fis = ctx.openFileInput(name);
			bm = BitmapFactory.decodeStream(fis);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return bm;
	}

	public static Bitmap createIcon(Context ctx, Bitmap[] cellBitmap) {
		Resources res = ctx.getResources();
		int iconSize = res.getDimensionPixelSize(R.dimen.app_icon_size);
		int iconSpan = res.getDimensionPixelSize(R.dimen.app_icon_span);
		int width = iconSize * 3 + iconSpan * 4;
		int height = iconSize * 3 + iconSpan * 4;
		Rect[] rects = initRects(iconSize, iconSpan);
		return mergeBitmap(ctx,width, height, cellBitmap, rects);
	}
	
	public static Bitmap createIcon(Context ctx, int[] cellResId) {
		Resources res = ctx.getResources();
		int iconSize = res.getDimensionPixelSize(R.dimen.app_icon_size);
		int iconSpan = res.getDimensionPixelSize(R.dimen.app_icon_span);
		int width = iconSize * 3 + iconSpan * 4;
		int height = iconSize * 3 + iconSpan * 4;
		Rect[] rects = initRects(iconSize, iconSpan);
		return mergeBitmap(ctx,width, height, cellResId, rects);
	}
	
	private static Rect[] initRects(int size, int span) {
		Rect[] rects = new Rect[MAX_ICON_NUM];
		for(int i= 0 ;i<MAX_ICON_NUM;i++){
			rects[i] = new Rect();
		}
		// row 0
		rects[0].left = span;
		rects[0].top = span;
		rects[0].right = rects[0].left + size;
		rects[0].bottom = rects[0].top + size;

		rects[1].left = rects[0].right + span;
		rects[1].top = span;
		rects[1].right = rects[1].left + size;
		rects[1].bottom = rects[1].top + size;

		rects[2].left = rects[1].right + span;
		rects[2].top = span;
		rects[2].right = rects[2].left + size;
		rects[2].bottom = rects[2].top + size;

		// row 1
		rects[3].left = span;
		rects[3].top = rects[0].bottom + span;
		rects[3].right = rects[3].left + size;
		rects[3].bottom = rects[3].top + size;

		rects[4].left = rects[3].right + span;
		rects[4].top = rects[3].top;
		rects[4].right = rects[4].left + size;
		rects[4].bottom = rects[4].top + size;

		rects[5].left = rects[4].right + span;
		rects[5].top = rects[4].top;
		rects[5].right = rects[5].left + size;
		rects[5].bottom = rects[5].top + size;

		// row 2
		rects[6].left = span;
		rects[6].top = rects[3].bottom + span;
		rects[6].right = rects[6].left + size;
		rects[6].bottom = rects[6].top + size;

		rects[7].left = rects[6].right + span;
		rects[7].top = rects[6].top;
		rects[7].right = rects[7].left + size;
		rects[7].bottom = rects[7].top + size;

		rects[8].left = rects[7].right + span;
		rects[8].top = rects[7].top;
		rects[8].right = rects[8].left + size;
		rects[8].bottom = rects[8].top + size;
		return rects;
	}

	public static Bitmap mergeBitmap(Context ctx,int width, int height,
			Bitmap[] cellBitmap, Rect[] rect) {
		Bitmap bitmap = Bitmap.createBitmap(UITools.dip2px(ctx, 48), UITools.dip2px(ctx, 48), Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		    
		for (int i = 0; i < cellBitmap.length; i++) {
			canvas.drawBitmap(cellBitmap[i], null, rect[i], null);
		}
		
        Paint paint=new Paint(); 	
		Bitmap bg = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.widget_icon_bg);  
        canvas.drawBitmap(bg, 0,0, paint); 
		return bitmap;
	}

	public static Bitmap mergeBitmap(Context contex, int width, int height, int[] cellDrawable,
			Rect[] rect) {
		Resources res = contex.getResources();
		Bitmap[] cellBitmap = new Bitmap[cellDrawable.length];
		for (int i = 0; i < cellDrawable.length; i++) {
			cellBitmap[i] = BitmapFactory.decodeResource(res, cellDrawable[i]);
		}
		return mergeBitmap(contex,width, height, cellBitmap, rect);
	}

//	public static Bitmap mergeBitmap(int width, int height,
//			Drawable[] cellIcon, Rect[] rect) {
//		Bitmap[] cellBitmap = new Bitmap[cellIcon.length];
//		for (int i = 0; i < cellIcon.length; i++) {
//			cellBitmap[i] = drawable2Bitmap(cellIcon[i]);
//		}
//		return mergeBitmap(width, height, cellBitmap, rect);
//	}

	public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
		int w = bitmap.getWidth();
		int h = bitmap.getHeight();
		Matrix matrix = new Matrix();
		float scaleWidth = ((float) width / w);
		float scaleHeight = ((float) height / h);
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
		return newbmp;
	}

	public static Bitmap zoomBitmap(Bitmap bitmap, int percentage) {
		int w = bitmap.getWidth();
		int h = bitmap.getHeight();
		Matrix matrix = new Matrix();
		float scaleWidth = ((float) w * percentage);
		float scaleHeight = ((float) h * percentage);
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
		return newbmp;
	}
	
	public Bitmap bytes2Bimap(byte[] b) {
		if (b.length != 0) {
			return BitmapFactory.decodeByteArray(b, 0, b.length);
		} else {
			return null;
		}
	}

	public byte[] bitmap2Bytes(Bitmap bm) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
		return baos.toByteArray();
	}


	public static Drawable bitmap2Drawable(Bitmap bitmap, Context contex) {
		Resources res = contex.getResources();
		BitmapDrawable bd = new BitmapDrawable(res, bitmap);
		return bd;
	}

	/**
	 * Drawable 转 bitmap
	 * 
	 * @param drawable
	 * @return
	 */
	public static Bitmap drawable2Bitmap(Drawable drawable) {
		if (drawable instanceof BitmapDrawable) {
			return ((BitmapDrawable) drawable).getBitmap();
		} else if (drawable instanceof NinePatchDrawable) {
			Bitmap bitmap = Bitmap
					.createBitmap(
							drawable.getIntrinsicWidth(),
							drawable.getIntrinsicHeight(),
							drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
									: Bitmap.Config.RGB_565);
			Canvas canvas = new Canvas(bitmap);
			drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
					drawable.getIntrinsicHeight());
			drawable.draw(canvas);
			return bitmap;
		} else {
			return null;
		}
	}

	public static void saveBitmap(Bitmap bitmap, String path) {
		FileOutputStream out = null;
		try {
			File file = new File(path);
			out = new FileOutputStream(file);
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
			out.flush();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
